package com.cooldatasoft.app;

import javax.servlet.http.HttpServletRequest;

import org.apache.wicket.Application;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadWebRequest;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.proxy.LazyInitProxyFactory;
import org.apache.wicket.spring.ISpringContextLocator;
import org.apache.wicket.spring.SpringBeanLocator;
import org.apache.wicket.spring.SpringWebApplication;
import org.springframework.context.ApplicationContext;

import com.cooldatasoft.service.NodeService;
import com.cooldatasoft.service.PersonService;
import com.cooldatasoft.service.UserService;
import com.cooldatasoft.service.WorkService;
import com.cooldatasoft.web.cms.login.LoginPage;
/**
 * 
 * @author fatih mehmet ucar
 *
 */
public class WOCApplication extends SpringWebApplication{

	public static int MAX_FILE_SIZE_KB = 1000000;
	
	
	public static WOCApplication get(){
		return (WOCApplication)Application.get();
	}
	@Override
	public Class getHomePage() {	
		return LoginPage.class;		
	}

	@Override
	public final Session newSession(Request request, Response response) {
		return new WOCSession(request);
	}

	@Override
	protected void init() {
		super.init();
		getMarkupSettings().setStripWicketTags(true);
	}


	
	/**
	 * Added to Make upload progress bar function properly.
	 */
	@Override 
	protected WebRequest newWebRequest(HttpServletRequest servletRequest) { 
		return new UploadWebRequest(servletRequest);
	}
	
	
	/**
	 * Creates a proxy for a spring bean that is safe to put into session and
	 * serialize
	 *
	 * @param clazz class of spring bean
	 * @param beanName name of spring bean
	 * @return proxy representing the spring bean
	 */
	@Override
	protected Object createSpringBeanProxy(Class clazz, String beanName) {
		return LazyInitProxyFactory.createProxy(clazz, new SpringBeanLocator(beanName, clazz, getSpringContextLocator()));
	}

	private final static ISpringContextLocator contextLocator = new ISpringContextLocator() {

		private static final long serialVersionUID = 1L;

		public ApplicationContext getSpringContext() {
			Application app = Application.get();
			return ((WOCApplication) app).internalGetApplicationContext();
		}
	};
	public static final int NUMBER_OF_RECORD_PER_PAGE = 20;
	/**
	 * Retrieves the spring application context locator object
	 * @return spring application context locator object
	 */
	public ISpringContextLocator getSpringContextLocator() {
		return contextLocator;
	}
	public Object getSpringBean(String beanName){
		return internalGetApplicationContext().getBean(beanName);
	}
	
	
	
	
	
	// ************Services****************
	
	/**
	 * this field holds a service proxy that is safe to use in wicket
	 * components
	 */
	private UserService userServiceProxy;
	private PersonService personServiceProxy;
	private WorkService workServiceProxy;
	private NodeService nodeServiceProxy;
	
	
	public UserService getUserServiceProxy() {		
		if (userServiceProxy == null) {
			synchronized (this) {
				if (userServiceProxy == null) {
					userServiceProxy = (UserService) createSpringBeanProxy(UserService.class, "UserService");
				}
			}
		}
		return userServiceProxy;
	}
	
	public PersonService getPersonServiceProxy() {		
		if (personServiceProxy == null) {
			synchronized (this) {
				if (personServiceProxy == null) {
					personServiceProxy = (PersonService) createSpringBeanProxy(PersonService.class, "PersonService");
				}
			}
		}
		return personServiceProxy;
	}
	
	public WorkService getWorkServiceProxy() {		
		if (workServiceProxy == null) {
			synchronized (this) {
				if (workServiceProxy == null) {
					workServiceProxy = (WorkService) createSpringBeanProxy(WorkService.class, "WorkService");
				}
			}
		}
		return workServiceProxy;
	}

	public NodeService getNodeServiceProxy() {		
		if (nodeServiceProxy == null) {
			synchronized (this) {
				if (nodeServiceProxy == null) {
					nodeServiceProxy = (NodeService) createSpringBeanProxy(NodeService.class, "NodeService");
				}
			}
		}
		return nodeServiceProxy;
	}

	

}
